// 1、常用的内置对象
// Date Math Array String Object Function
let times = new Date().getTime()
// console.log(times)
// yyyy-mm-dd hh:MM:ss
function formatTime() {
  let date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let seconde = date.getSeconds()
  return (
    year +
    '-' +
    padStart(month) +
    '-' +
    padStart(day) +
    ' ' +
    padStart(hour) +
    ':' +
    padStart(minute) +
    ':' +
    padStart(seconde)
  )
}

// v = 1
function padStart(v) {
  v = v.toString()
  return v[1] ? v : '0' + v
}

console.log(formatTime())

onload = function () {
  let timeElement = document.getElementsByClassName('time')[0]
  setInterval(function () {
    let str = formatTime()
    timeElement.innerHTML = str
  }, 1000)
}

// 2、数据类型
// object boolean number null undefined string function bigint sysmbol
function fn(a, b) {
  return a + b
}
console.log(typeof (1 * 's')) // NaN: not a number

// 3、对象
let object = {
  id: 1,
  name: 'tom',
  age: 20,
  show() {
    console.log('{' + ' id:' + this.id + ',name:' + this.name + ',age:' + this.age + '}')
  },
}
object.show()
let obj = object
obj.show()
obj.age = 21
console.log(object.age) // 20 21 ?

// 4、JSON 对象转字符串 JSON.stringify(object)
let json = {
  name: 'tom',
  id: 1,
  age: 20,
  married: false,
}
// 把对象转换成字符串
let str = JSON.stringify(json)
// {"name":"tom","id":1,"age":20,"married":false}
console.log(str)
// 模板字符串
let age = 20
let objectStr = `{"name":"tom","id":1,"age":${age},"married":false}`
console.log(objectStr)
// 把字符串转换成对象 JSON.parse(object)
let strToObject = JSON.parse(str)
console.log(strToObject.name)

// 5、Math相关的api
// 随机产生一个1-10之间的整数
// 1<= parseInt(Math.random()*10)+1 <11 --->[1,10]
// [min,max] = parseInt(Math.random()*(max-min+1)))+min
// [18,25]
/*
  18<=parseInt(Math.random()*8)+18<=7+18
*/
let arr = []
for (let i = 0; i < 10; i++) {
  let n = parseInt(Math.random() * (25 - 18 + 1)) + 18
  arr.push(n)
}
console.log(arr)
console.log(Math.max(10, 2, -1, 3))
console.log(Math.min(10, 2, -1, 3))

// 6、Array数组常用的api
let array = [,]
// 添加
array.push(1)
array.push(null)
array.push(strToObject)
console.log(array) // [empty,1,null,obj]
// 删除元素splice(index,n),在index开始删除n个元素
let rt = array.splice(1, 1)
console.log(rt, array)
// 插入splice(1, 0, 10) 1是元素的下标，0表示删除0个，在index=1位置添加元素10
array.splice(1, 0, 10)
console.log(array)
// 修改
array[1] = 100
console.log(array)
// 找元素的下标
let index = array.indexOf(100)
console.log(array[index])
// 冒泡排序：外层循环n-1,内存循环n-i-1 ,内存循环值交换
array = [-100, -200, 0, 20, -1]
// for (let i = 0, n = array.length; i < n - 1; i++) {
//   for (let j = 0; j < n - i - 1; j++) {
//     if (array[j] < array[j + 1]) {
//       let t = array[j]
//       array[j] = array[j + 1]
//       array[j + 1] = t
//     }
//   }
// }
array = array.sort(function (a, b) {
  return b - a
})
console.log(array)
// 求最大和最小值
let min = array[0],
  max = array[0]
for (let i = 0; i < array.length; i++) {
  if (min > array[i]) {
    min = array[i]
  }
  if (max < array[i]) {
    max = array[i]
  }
}
console.log(min, max)
